<?php

/**
 * Framework_Presenter_Smarty_SearchBar
 *
 * @author Application Support Group
 * @copyright University of Georgia
 * @license http://www.opensource.org/licenses/gpl-license.php
 * @package BAMF
 * @filesource
*/


/**
 * Framework_Presenter_Smarty_SimpleSearchBar
 *
 *
 * @author Application Support Group
 * @package BAMF
 * @link http://smarty.php.net
*/
class Framework_Presenter_Smarty_SimpleSearchBar extends Framework_Presenter_Smarty_Extension {

	public function __construct() {
		parent::__construct() ;
	}

	public static function register( &$smartyObj ) {
		$smartyExtension = new Framework_Presenter_Smarty_SimpleSearchBar() ;
		$smartyObj->register_function( "simpleSearchBar", array( $smartyExtension, "simpleSearchBar" )) ;
	}


// build a search bar for the given collection, format parameters, and state

	public function simpleSearchBar($params, &$smarty)
	{
		$_output = array() ;
		
		extract($params);
	
		if (empty($column)) {
			$smarty->trigger_error("simpleSearchBar: missing 'column' parameter");
			return;
		}

		if (empty($collection)) {
			$smarty->trigger_error("simpleSearchBar: missing 'collection' parameter");
			return;
		}

		$collectionParameters = $collection->getParameters() ;
		$searchState = unserialize( $collectionParameters->rawSearchParams ) ;
	
// We use the fields defined in the collection schema unless they're being overridden by the format array
		
		$modulePath = $smarty->get_template_vars( 'modulePath' ) ;
		$itemNumber = $smarty->get_template_vars( 'itemNumber' ) ;
		$formAction = empty( $itemNumber ) ? $modulePath : $modulePath . "/" . $itemNumber ;

// data types for each column these drive the (dis)appearance of the various value form elements
		
	
		$_output[] = '<div class="searchBar">' ;
			
		$_output[] = '	<form name="'. SBAR_FORM_NAME. '" method="post" action="'. $formAction . '">' ;
		$_output[] = '		<input type="hidden" name="' . SBAR_KEY_SEARCH_COLUMN . '" value="'. $column .'" />' ;
		$_output[] = '		<input type="hidden" name="' . SBAR_KEY_SEARCH_1 . '" value="" />' ;
		
		$_output[] = "<p>Search:" ;

		$_output[] = '			 

			<input type="text" name="'.SBAR_KEY_SEARCH_0.'" value="' . htmlspecialchars($searchState[ "search_value_0" ][0]) . '" size="20" maxlength="40" />
				 
				 &nbsp;&nbsp;<input type="submit" name="' . SBAR_KEY_SUBMIT . '" value="' . SBAR_VAL_SUBMIT . '" />
				 &nbsp;&nbsp;<input type="submit" name="' . SBAR_KEY_RESET . '" value="' . SBAR_VAL_RESET . '" onclick="document.forms[\''.SBAR_FORM_NAME.'\'].elements[\''.SBAR_KEY_SEARCH_0.'\'].value=\'\';" />
				 </p>
			</form>
		</div>
		' ;


		return implode( "\n", $_output ) ;


	}

	public function __destruct() {

	}


}
